package com.example.grape_utils;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class PermissionsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPermissions();
        requestPermissions();
    }

    ArrayList<String> permissions = new ArrayList<>();
    private final int permissionRequestCode = 100;
    public void addPermissions() {
        permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        permissions.add(Manifest.permission.CAMERA);
        permissions.add(Manifest.permission.RECORD_AUDIO);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            checkStorageManagerPermission();
        }
    }
    protected void requestPermissions() {
        for (String permission : permissions) {
            int result = checkSelfPermission(permission);
            if (result == PackageManager.PERMISSION_DENIED) {
                requestPermissions( permissions.toArray(new String[permissions.size()]), permissionRequestCode);
                return;
            }
        }
        onDonePermissionGranted();
    }
    private void checkStorageManagerPermission() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R ||
                Environment.isExternalStorageManager()
        ) {
            Toast.makeText(this, "已获得访问所有文件权限", Toast.LENGTH_SHORT).show();
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this)
                    .setMessage("本程序需要您同意允许访问所有文件权限")
                    .setPositiveButton("确定",  new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                            startActivity(intent);
                        }
                    });
             builder.show();
        }
    }


    public void onDonePermissionGranted() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == permissionRequestCode) {
            for (int i = 0 ; i < grantResults.length; i++) {
                int result = grantResults[i];
                if (result == PackageManager.PERMISSION_DENIED) {
                    Toast.makeText(this, "grand failed !" + permissions[i], Toast.LENGTH_LONG).show();
                    return;
                }
            }
            onDonePermissionGranted();
        }
    }
}